<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-title">
        <h2>体育场馆预约系统</h2>
        <p>管理员登录</p>
      </div>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'



export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginForm = reactive({
      username: '',
      password: ''
    })
    const loading = ref(false)
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
      ]
    }
    
    const loginFormRef = ref(null)
    
    const handleLogin = () => {
      if (loginFormRef.value) {
      loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            // 调用登录API
            const response = await request.post('/v1/admin/login', {
              username: loginForm.username,
              password: loginForm.password
            })
            
              console.log('登录响应:', response)
              
              // 正确解析返回数据 - request实例已经处理过响应，直接使用response
              const { data } = response
            
              if (data && data.token) {
            // 存储token和用户信息
                localStorage.setItem('admin_token', data.token)
                localStorage.setItem('admin_username', data.admin.username)
            
            ElMessage({
              message: '登录成功',
              type: 'success'
            })
            
            // 跳转到首页
            router.push('/')
              } else {
                ElMessage.error('登录响应格式错误')
              }
          } catch (error) {
              console.error('登录错误:', error)
            ElMessage.error(error.response?.data?.message || '登录失败，请检查用户名和密码')
          } finally {
            loading.value = false
          }
        }
      })
      } else {
        ElMessage.error('表单引用错误')
      }
    }
    
    // 清除之前的登录信息
    onMounted(() => {
      localStorage.removeItem('admin_token')
      localStorage.removeItem('admin_username')
    })
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      handleLogin
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
  
  .login-form {
    width: 350px;
    padding: 30px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    
    .login-title {
      text-align: center;
      margin-bottom: 30px;
      
      h2 {
        font-size: 24px;
        color: #303133;
        margin-bottom: 10px;
      }
      
      p {
        font-size: 14px;
        color: #606266;
      }
    }
    
    .login-button {
      width: 100%;
    }
  }
}
</style> 